local sk__fushe = fk.CreateSkill {

  name = "sk__fushe",

  tags = {},

}



sk__fushe:addEffect(fk.EventPhaseStart, {
  name = "sk__fushe",
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if target.phase == Player.Play then
      return player:hasSkill(sk__fushe.name) and not target.dead and player:inMyAttackRange(target)
    end
  end,
  on_cost = function(self, event, target, player, data)
    local choices = table.map({ "spade", "heart", "club", "diamond" }, function(s)
      return U.ConvertSuit(s, "str", "sym")
    end)
    table.insert(choices, "Cancel")
    local choice = player.room:askForChoice(player, choices, sk__fushe.name, "#sk__fushe-invoke::" .. target.id)
    if choice ~= "Cancel" then
      event:setCostData(self, choice)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(sk__fushe.name)
    room:doIndicate(player.id, { target.id })
    room:notifySkillInvoked(player, sk__fushe.name, "control")
    U.setPrivateMark(player, "sk__fushe-phase", { event:getCostData(self) })
  end,
})
sk__fushe:addEffect(fk.EventPhaseEnd, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if target.phase == Player.Play then
      if player:usedSkillTimes(sk__fushe.name, Player.HistoryPhase) > 0 then
        local suit = U.ConvertSuit(player:getMark("@[private]sk__fushe-phase").value[1], "sym", "int")
        return #player.room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
          for _, move in ipairs(e.data) do
            if move.toArea == Card.DiscardPile then
              for _, info in ipairs(move.moveInfo) do
                if Fk:getCardById(info.cardId).suit == suit then
                  return true
                end
              end
            end
          end
        end, Player.HistoryPhase) > 0
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(sk__fushe.name)
    room:doIndicate(player.id, { target.id })
    room:notifySkillInvoked(player, sk__fushe.name, "offensive")
    player:drawCards(1, sk__fushe.name)
    if not target.dead then
      room:damage({
        from = player,
        to = target,
        damage = 1,
        skillName = sk__fushe.name,
      })
    end
  end,
})

return sk__fushe
